import { defineStore } from 'pinia'
import Storage from '@/libs/storage'
import { useAmapKeyStore } from '../amapKey'

const storage = new Storage()
export const useGDRegionDataStore = defineStore('GDRegionDataStore', {
  state: (): { GD_RegionData: RegionData[] } => {
    return {
      GD_RegionData: storage.getItem('GD_RegionData') || [],
    }
  },
  actions: {
    SET_GD_RegionData(payload: RegionData[]) {
      this.GD_RegionData = payload
      storage.setItem('GD_RegionData', this.GD_RegionData, 60 * 60 * 24)
    },
    DEL_GD_RegionData() {
      this.GD_RegionData = []
      storage.removeItem('GD_RegionData')
    },
  },
  getters: {
    getterGDRegionData: (state) => {
      return state.GD_RegionData
    },
  },
})

interface RegionData {
  adcode: string
  center: string
  citycode: string
  districts: RegionData[]
  name: string
  level: string
  children: RegionData[]
  label: string
  value: string
}
export const asyncGetGDRegionData = async () => {
  const localData = storage.getItem('GD_RegionData')
  if (localData) {
    return
  }
  const res = await fetch('https://restapi.amap.com/v3/config/district', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },

    body: `key=${useAmapKeyStore().getAmapWebServiceKey}&keywords=&subdistrict=3&extensions=base`,
  }).then((res) => res.json())
  if (res.status === '1' && res.info === 'OK' && res.infocode === '10000') {
    const data: RegionData[] = res.districts[0].districts
    data.forEach((item) => {
      if (item.citycode.length > 0 && item.districts.length === 1) {
        item.districts = item.districts[0].districts
      } else if (item.districts.length === 0) {
        item.districts.push({
          adcode: '',
          districts: [],
          name: '',
          center: '',
          children: [],
          citycode: '',
          level: '',
          label: '',
          value: '',
        })
      }
    })
    const handle = (regionData: RegionData[]) => {
      regionData.forEach((item) => {
        item.children = JSON.parse(JSON.stringify(item.districts))
        item.label = item.name
        item.value = item.adcode
        if (item.children.length > 0) {
          handle(item.children)
        }
        Reflect.deleteProperty(item, 'center')
        Reflect.deleteProperty(item, 'citycode')
        Reflect.deleteProperty(item, 'districts')
        Reflect.deleteProperty(item, 'level')
        Reflect.deleteProperty(item, 'name')
        Reflect.deleteProperty(item, 'adcode')
      })
    }
    handle(data)
    useGDRegionDataStore().SET_GD_RegionData(data)
  }
}
